package com.eagle.spring.reactive.loader;

import java.util.concurrent.TimeUnit;

public class DataLoader {
	public final void load() {
		long startTime = System.currentTimeMillis(); // 开始时间
		doLoad(); // 具体执行
		long costTime = System.currentTimeMillis() - startTime; // 消耗时间
		System.out.println("load() 总耗时：" + costTime + " 毫秒");
	}
	protected void doLoad() { // 串行计算
		loadConfigurations();    //  耗时 1s
		loadUsers();                  //  耗时 2s
		loadOrders();                // 耗时 3s
	}// 总耗时 1s + 2s  + 3s  = 6s

	protected final String loadConfigurations() {
		return loadMock("loadConfigurations()", 1);
	}

	protected final String loadUsers() {
		return loadMock("loadUsers()", 2);
	}

	protected final String loadOrders() {

		return loadMock("loadOrders()", 3);
	}

	private String loadMock(String source, int seconds) {
		try{
			long startTime = System.currentTimeMillis();
			long milliseconds = TimeUnit.SECONDS.toMillis(seconds);
			Thread.sleep(milliseconds);
			long costTime = System.currentTimeMillis() - startTime;
			System.out.printf("[线程 : %s] %s 耗时 :  %d 毫秒\n",
					Thread.currentThread().getName(), source, costTime);
		} catch (Exception e){
			throw new RuntimeException(e);
		}
		return source;
	}

	public static void main(String[] args) {
		new DataLoader().load();
	}

}
